//
//  LoginViewController.m
//  HuiPinZhiJia
//
//  Created by MS on 16/9/7.
//  Copyright © 2016年 MS. All rights reserved.
//

#import "LoginViewController.h"
#import "AppDelegate.h"
#import "AFNetWorking.h"
@interface LoginViewController ()
@property (weak, nonatomic) IBOutlet UITextField *userName;
@property (weak, nonatomic) IBOutlet UITextField *passWord;
@property (nonatomic,strong) MBProgressHUD *HUD;
@end

@implementation LoginViewController

- (void)viewWillAppear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = YES;
}

- (void)viewWillDisappear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = NO;
}
- (IBAction)loginBtn:(id)sender {
    __weak typeof(self) weakSelf=self;

    NSString *name = [_userName.text stringByReplacingOccurrencesOfString:@" " withString:@""];
    NSString *passWord = [_passWord.text stringByReplacingOccurrencesOfString:@" " withString:@""];
    name.length==0?({
        MBProgressHUD * HUD=[(AppDelegate *)[UIApplication sharedApplication].delegate responseWithString:@"用户名不能为空!"];
        [HUD hide:YES afterDelay:0.5];
    }):(passWord.length==0?({
        MBProgressHUD * HUD=[(AppDelegate *)[UIApplication sharedApplication].delegate responseWithString:@"密码不能为空!"];
        [HUD hide:YES afterDelay:0.5];
    }):({
        if ([AFNetworkReachabilityManager sharedManager].networkReachabilityStatus==AFNetworkReachabilityStatusNotReachable) {
            [(AppDelegate *)[UIApplication sharedApplication].delegate NetState];
        }else{
            MBProgressHUD * HUD=[(AppDelegate *)[UIApplication sharedApplication].delegate responseWithString:@"正在登录..."];
            _HUD = HUD;
            [weakSelf sendRequestWithName:name password:passWord];
        }
    }));
}
-(void)sendRequestWithName:(NSString *) name password:(NSString *) password{
    __weak typeof(self) weakSelf=self;
    [[NetWorking sharedManager] postParameters:@{@"username":name,@"password":password} type:[NetWorking sharedManager].login progressValue:^(double value){
    } success:^(id info){
        SLLog(@"%@",info);
        if ([info[@"status"] integerValue]==1) {
            weakSelf.HUD.labelText=@"登录成功!";
            [weakSelf.HUD hide:YES afterDelay:1];
            [[NSUserDefaults standardUserDefaults] setObject:name forKey:@"userName"];
            [[NSUserDefaults standardUserDefaults] setObject:password forKey:@"userPassword"];
            [[NSUserDefaults standardUserDefaults] setObject:info[@"data"][@"token"] forKey:@"token"];
            [[NSUserDefaults standardUserDefaults] setObject:@(YES) forKey:@"isAutoLogin"];
            [[NSUserDefaults standardUserDefaults] setObject:@(YES) forKey:@"isLoginSuccess"];
            [[NSUserDefaults standardUserDefaults] synchronize];
            //实名认证
            [[NSUserDefaults standardUserDefaults]setObject:info[@"data"][@"is_certification"] forKey:@"is_certification"];
        }else{
            weakSelf.HUD.labelText=info[@"info"];
            [weakSelf.HUD hide:YES afterDelay:1];
        }
    }failure:^(id info){
        weakSelf.HUD.labelText=@"网络错误!";
        [weakSelf.HUD hide:YES afterDelay:1];
    }];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
